import React from 'react';
import jspdf from 'jspdf';

export default () => {
    const [img, setImg] = React.useState('')
    const downloadPdf = () => {
        const doc = new jspdf()
        doc.text("Hello semlinker!", 66, 88);
        const blob = new Blob([doc.output()], { type: "application/pdf" });
        const link = document.createElement('a')
        link.href = URL.createObjectURL(blob)
        link.download = 'test.pdf'
        link.click()
        link.remove()
        URL.revokeObjectURL(link.href)
    }
    const onFileChange = (event: any) => {
        const reader = new FileReader()
        reader.onload = function () {
            setImg(reader.result as string)
        }
        reader.readAsDataURL(event.target.files[0])
    }
    const downloadPdf2 = () => {
        const doc = new jspdf()
        doc.setFontSize(40);
        doc.text('Paranyan loves jsPDF', 35, 25);
        doc.addImage(img, 'JPEG', 15, 40, 180, 160);
        const blob = new Blob([doc.output()], { type: "application/pdf" });
        const link = document.createElement('a')
        link.href = URL.createObjectURL(blob)
        link.download = 'test.pdf'
        link.click()
        link.remove()
        URL.revokeObjectURL(link.href)
    }
    return (
        <div>
            <button onClick={downloadPdf}>导出pdf</button>
            <input type="file" onChange={onFileChange}></input>
            <button onClick={downloadPdf2}>导出图片pdf</button>
        </div>
    )
}